<?php

if (version_compare(phpversion(), '5', '<') === true)
{
    exit('phpFox 3.x requires PHP 5 or newer.');
}

define('PHPFOX', true);
define('PHPFOX_DS', DIRECTORY_SEPARATOR);
define('PHPFOX_DIR', dirname(__FILE__) . PHPFOX_DS);
require (PHPFOX_DIR . 'include' . PHPFOX_DS . 'init.inc.php');
require (PHPFOX_DIR . 'include' . PHPFOX_DS . 'setting' . PHPFOX_DS . 'server.sett.php');
$moduleID = "topcmmchat";
$db = new Phpfox_Database_Driver_Mysql;
if(!$db -> connect($_CONF['db']['host'], $_CONF['db']['user'], $_CONF['db']['pass'],$_CONF['db']['name']))
{
    echo 'connect error';
    exit;
}
$table = Phpfox::getT('product');
$select = "select * from " . $table . " where ;";
$aRows = $db->select('*')
                    ->from($table)
                    ->where("product_id = 'phpfox_123flashchat'")
                    ->execute('getRows');

if (!empty($aRows))
{
    echo 'You have installed this module.';
    exit;
}
$table = Phpfox::getT('chat_settings');
$select = "SELECT 'fc_extendserver' FROM " . $table . " WHERE id = '1'";
if (!$db -> query($select))
{
    $create = 'CREATE TABLE IF NOT EXISTS ' . $table . ' (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fc_extendserver` tinyint(1) NOT NULL,
  `fc_server_host` varchar(255) COLLATE utf8_bin NOT NULL,
  `fc_server_port` int(11) NOT NULL,
  `fc_http_port` int(11) NOT NULL,
  `fc_client_loc` varchar(255) COLLATE utf8_bin NOT NULL,
  `fc_api_url` varchar(255) COLLATE utf8_bin NOT NULL,
  `fc_group` varchar(255) COLLATE utf8_bin NOT NULL,
  `fc_room` varchar(255) COLLATE utf8_bin NOT NULL,
  `fc_room_list` tinyint(1) NOT NULL,
  `fc_user_list` tinyint(1) NOT NULL,
  `fc_fullscreen` tinyint(1) NOT NULL,
  `fc_client_lang` varchar(10) COLLATE utf8_bin NOT NULL,
  `fc_client_skin` varchar(10) COLLATE utf8_bin NOT NULL,
  `fc_client_width` int(11) NOT NULL,
  `fc_client_height` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);';
    if (!$db -> query ($create))
    {
        echo 'Create chat settings table failure.';
        exit;
    }

    $insert = "INSERT INTO " . $table . " (`id`, `fc_extendserver`, `fc_server_host`, `fc_server_port`, `fc_http_port`, `fc_client_loc`, `fc_api_url`, `fc_group`, `fc_room`, `fc_room_list`, `fc_user_list`, `fc_fullscreen`, `fc_client_lang`, `fc_client_skin`, `fc_client_width`, `fc_client_height`) VALUES
(1, 2, '".$_SERVER['SERVER_NAME']."', 51127, 35555, 'http://".$_SERVER['SERVER_NAME'].":35555/', 'http://".$_SERVER['SERVER_NAME'].":35555/', 'default', 'Lobby', 1, 1, 1, 'auto', 'default', 800, 600);";
    if (!$db -> query ($insert))
    {
        echo 'Add chat settings failure.';
        exit;
    }
}



$table = Phpfox::getT('product');
$insert = "INSERT INTO " . $table . " (`product_id`, `is_core`, `title`, `description`, `version`, `is_active`, `url`, `url_version_check`) VALUES 
('phpfox_123flashchat', 0, 'phpfox mod for 123flashchat', 'This product installs a 123flashchat mod for you.', '3.0.0', 1, 'http://www.123flashchat.com', NULL);";
if (!$db -> query($insert))
{
    echo 'Create product failure.';
    exit;
}
$table = Phpfox::getT('module');
$insert = "INSERT INTO " . $table . " (`module_id`, `product_id`, `is_core`, `is_active`, `is_menu`, `menu`, `phrase_var_name`) VALUES 
('$moduleID', 'phpfox_123flashchat', 0, 1, 1, 'a:2:{s:35:\"topcmmchat.admin_menu_chat_settings\";a:1:{s:3:\"url\";a:1:{i:0;s:10:\"topcmmchat\";}}s:38:\"topcmmchat.admin_menu_chat_admin_panel\";a:1:{s:3:\"url\";a:2:{i:0;s:10:\"topcmmchat\";i:1;s:5:\"admin\";}}}', 'module_chat');";
if (!$db -> query($insert))
{
    echo 'Create module failure.';
    exit;
}

$table = Phpfox::getT('component');
$insert = "INSERT INTO " . $table . " (`component`, `m_connection`, `module_id`, `product_id`, `is_controller`, `is_block`, `is_active`) VALUES 
('index', 'topcmmchat.index', '$moduleID', 'phpfox_123flashchat', 1, 0, 1), 
('chatroom', 'topcmmchat.chatroom', '$moduleID', 'phpfox_123flashchat', 1, 0, 1), 
('dashboard', NULL, '$moduleID', 'phpfox_123flashchat', 0, 1, 1);";
if (!$db -> query($insert))
{
    echo 'Create Component failure.';
    exit;
}

$table = Phpfox::getT('block');
$insert = "INSERT INTO " . $table . " (`m_connection`, `module_id`, `product_id`, `component`, `location`, `is_active`, `ordering`, `disallow_access`, `can_move`, `version_id`) VALUES 
('core.index-visitor', '$moduleID', 'phpfox_123flashchat', 'dashboard', 1, 1, 1, NULL, 1, '2'), ('core.index-member', '$moduleID', 'phpfox_123flashchat', 'dashboard', 1, 1, 1, NULL, 1, '2');";
if (!$db -> query($insert))
{
    echo 'Create Block failure.';
    exit;
}

$table = Phpfox::getT('language_phrase');
$insert = "INSERT INTO " . $table . " (`language_id`, `module_id`, `product_id`, `version_id`, `var_name`, `text`, `text_default`, `added`) VALUES 
('en', '$moduleID', 'phpfox_123flashchat', '2.0.0rc1', 'admin_menu_chat_settings', 'Chat Settings', 'Chat Settings', " . date('U') . "),
('en', '$moduleID', 'phpfox_123flashchat', '2.0.0rc1', 'admin_menu_chat_admin_panel', 'Chat Admin Panel', 'Chat Admin Panel', " . date('U') . "),
('en', '$moduleID', 'phpfox_123flashchat', '2.0.0rc1', 'module_chat', 'Add chat module.', 'Add chat module.', " . date('U') . "),
('en', '$moduleID', 'phpfox_123flashchat', '2.0.0rc1', 'menu_123_flash_chat', '123 Flash Chat', '123 Flash Chat', " . date('U') . ");";
if (!$db -> query($insert))
{
    echo 'Insert Phrase failure.';
    exit;
}

$table = Phpfox::getT('menu');
$insert = "INSERT INTO " . $table . " (`parent_id`, `page_id`, `m_connection`, `module_id`, `product_id`, `var_name`, `is_active`, `ordering`, `url_value`, `disallow_access`, `version_id`) VALUES 
(0, 0, 'main', '$moduleID', 'phpfox_123flashchat', 'menu_123_flash_chat', 1, 90, 'topcmmchat', NULL, '2.0.0rc1');";
if (!$db -> query($insert))
{
    echo 'Create Menu failure.';
    exit;
}

$table = Phpfox::getT('page');
$delete = "delete from " . $table . " where title_url = 'topcmmchat'";
$db -> query($delete);
echo 'Install seccessfully.';

?>